﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YXH_Tools_Files.ObjectClass;
using YXH_Tools_Files.Tools_CSV;

namespace YXH_Tools_Files.Tools_Spike
{
    public static class RemoveSpikeProcess
    {
        /// <summary>
        /// 输出是去除spike后的某指定列的数据
        /// </summary>
        /// <param name="signal"></param>
        /// <param name="blockLengthMean"></param>
        /// <param name="blockLengthVariance"></param>
        /// <param name="percentage"></param>
        /// <param name="ratio"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static List<double> completeSpikeRemovalProcess(List<double> signal, int blockLengthMean, int blockLengthVariance, double percentage,double ratio,string filename)
        {
            //var re = Filter.MedianFilter(signal.ToArray(), 3);
            var smooth = Spike_Removal.meanSignalCal(signal, blockLengthMean);
            var roughr = Spike_Removal.createRoughSignal(signal, smooth);
            var mrr = Spike_Removal.MRiCal(roughr, blockLengthVariance);
            var variance = Spike_Removal.varianceSignalCal(roughr, mrr, blockLengthVariance);

            //var vs=variance.ConvertAll(d => d.ToString());
            //CSVOperator.YXHSaveCSVfromListOneTime($"D:\\TEST\\report\\myvariance.txt", vs);

            //var norvariance = variance.MinMaxNormalization();

            //var vs2 = norvariance.ConvertAll(d => d.ToString());
            //CSVOperator.YXHSaveCSVfromListOneTime($"D:\\TEST\\report\\mynormalvariance.txt", vs2);
            //检测spike并获取spike的序号
            var spikeindex = Spike_Removal.detectSpikesV2(variance, percentage);

            //var vs3= spikeindex.ConvertAll(d => d.ToString());
            //CSVOperator.YXHSaveCSVfromListOneTime($"D:\\TEST\\report\\myspike.txt", vs3);

            var groupspikeindex = Spike_Removal.GroupConsecutiveNumbers(spikeindex);
            var mergegroup = Spike_Removal.MergeConsecutiveGroups(groupspikeindex);
            var spikeexactindex = Spike_Removal.getAccurateSpikeIndexV2(signal, mergegroup);
            var interceptvalue = Spike_Removal.getratioFilterValue(signal, spikeexactindex, ratio);
            var rmdata = Spike_Removal.removeSpike(signal, interceptvalue, spikeexactindex);
            if (rmdata != null)
            {
                return rmdata;
            }
            else
            {
                return signal;
            }
            //var vmax = variance.Max();
            //var vpecent = variance.Max() / (variance.Sum() / variance.Count);
            //if ((vpecent > 30 && vpecent < 50 && vmax > 0.04) || (vpecent >= 50 && vpecent <= 100 && vmax > 0.01) || vpecent > 100)
            //{
            //    //检测spike并获取spike的序号
            //    var spikeindex = Spike_Removal.detectSpikesV2(variance, percentage);
            //    var groupspikeindex = Spike_Removal.GroupConsecutiveNumbers(spikeindex);
            //    var mergegroup = Spike_Removal.MergeConsecutiveGroups(groupspikeindex);
            //    var spikeexactindex = Spike_Removal.getAccurateSpikeIndex(signal, mergegroup);
            //    var interceptvalue = Spike_Removal.getratioFilterValue(signal, spikeexactindex, ratio);
            //    var rmdata = Spike_Removal.removeSpike(signal, interceptvalue, spikeexactindex);
            //    return rmdata;
            //}
            ////直接返回原值，说明不用去除spike
            //return signal;



        }

        public static List<SpikeinFileClass>? completeSpikeRemovalProcessV2(List<double> signal, int blockLengthMean, int blockLengthVariance, double percentage, double ratio, int spikecolumn)
        {
            var smooth = Spike_Removal.meanSignalCal(signal, blockLengthMean);
            var roughr = Spike_Removal.createRoughSignal(signal, smooth);
            var mrr = Spike_Removal.MRiCal(roughr, blockLengthVariance);
            var variance = Spike_Removal.varianceSignalCal(roughr, mrr, blockLengthVariance);
            //检测spike并获取spike的序号
            var spikeindex = Spike_Removal.detectSpikesV2(variance, percentage);
            var groupspikeindex = Spike_Removal.GroupConsecutiveNumbers(spikeindex);
            var mergegroup = Spike_Removal.MergeConsecutiveGroups(groupspikeindex);
            var spikeexactindex = Spike_Removal.getAccurateSpikeIndexV2(signal, mergegroup);
            var interceptvalue = Spike_Removal.getratioFilterValue(signal, spikeexactindex, ratio);
            var result= Spike_Removal.GetspikeinFileClasses(spikeexactindex, spikecolumn,interceptvalue);
            return result;


        }

    }
}
